package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.MeetingCollectUser;
import com.ruoyi.system.domain.MeetingLeave;
import com.ruoyi.system.domain.MeetingManagement;
import com.ruoyi.system.domain.vo.UserUnDoVo;
import com.ruoyi.system.mapper.MeetingCollectUserMapper;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MeetingCollectMapper;
import com.ruoyi.system.domain.MeetingCollect;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会议收集Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-04
 */
@Service
public class MeetingCollectServiceImpl implements IMeetingCollectService
{
    @Autowired
    private MeetingCollectMapper meetingCollectMapper;
    @Autowired
    private IMeetingManagementService meetingManagementService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IMeetingCollectUserService meetingCollectUserService;
    @Autowired
    private MeetingCollectUserMapper meetingCollectUserMapper;
    @Autowired
    private IMeetingLeaveService meetingLeaveService;

    /**
     * 查询会议收集
     *
     * @param id 会议收集主键
     * @return 会议收集
     */
    @Override
    public MeetingCollect selectMeetingCollectById(Long id)
    {
        MeetingCollect meetingCollect = meetingCollectMapper.selectMeetingCollectById(id);
        String collectType = meetingCollect.getCollectType();
        //指定会议人员
        if("1".equals(collectType)){
            String collectUser = meetingCollect.getCollectUser();
            if(StringUtils.isNotBlank(collectUser)){
                List<String> userIdList = Arrays.asList(collectUser.split(","));
                ArrayList<SysUser> userList = new ArrayList();
                for(String userId : userIdList){
                    SysUser user = sysUserService.selectUserById(Long.valueOf(userId));
                    userList.add(user);
                }
                meetingCollect.setCollectUserList(userList);
                meetingCollect.setCollectUserIdList(userIdList);
            }
        }
        return meetingCollect;
    }

    /**
     * 查询会议收集列表
     *
     * @param meetingCollect 会议收集
     * @return 会议收集
     */
    @Override
    public List<MeetingCollect> selectMeetingCollectList(MeetingCollect meetingCollect)
    {
        List<MeetingCollect> meetingCollects = meetingCollectMapper.selectMeetingCollectList(meetingCollect);
        for(MeetingCollect vo : meetingCollects){
            MeetingCollectUser meetingCollectUser = new MeetingCollectUser();
            meetingCollectUser.setCollectId(vo.getId());
            List<MeetingCollectUser> meetingCollectUsers = meetingCollectUserService.selectMeetingCollectUserList(meetingCollectUser);
            int size = meetingCollectUsers.size();
            //收集人总数
            vo.setCollectSumCount(size);
            //已提交人数
            Integer countCounts = (int)meetingCollectUsers.stream().filter(o -> o.getStatus().equals("1")||o.getStatus().equals("2")).count();
            vo.setCollectCount(countCounts);
            //未提交人数
            Integer count = (int)meetingCollectUsers.stream().filter(o -> o.getStatus().equals("0")).count();
            vo.setNoCollectCount(count);
            String collectLeave = vo.getCollectLeave();
            //请假人员是否需要提交（0:不需要 1:需要）
            if("0".equals(collectLeave)){
                //查询会议请假人数
                MeetingLeave meetingLeave = new MeetingLeave();
                meetingLeave.setStatus("1");
                meetingLeave.setMeetingId(vo.getMeetingId());
                int leaveSize = meetingLeaveService.selectMeetingLeaveList(meetingLeave).size();
                //请假人员不需要提交时，收集人总数中排除请假人员
                vo.setCollectSumCount(size-leaveSize);
                //请假人员不需要提交时，未提交数据中排除请假人员
                vo.setDifferenceCount(count-leaveSize);
            }else{
                //还差多少人没有提交
                vo.setDifferenceCount(count);
            }
            //判断当前登录人是否在收集人范围内
            List<Long> userIdList = meetingCollectUsers.stream().map(o -> o.getUserId()).collect(Collectors.toList());
            Long userId = meetingCollect.getUserId();
            if(userIdList.contains(userId)){
                vo.setUpload(true);
            }else{
                vo.setUpload(false);
            }
        }
        return meetingCollects;
    }

    /**
     * 新增会议收集
     *
     * @param meetingCollect 会议收集
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertMeetingCollect(MeetingCollect meetingCollect)
    {
        meetingCollect.setCreateTime(DateUtils.getNowDate());
        String collectUserType = meetingCollect.getCollectUserType();
        //收集人
        List<String> userIdList = new ArrayList<>();
        //全体会议党员
        if("0".equals(collectUserType)){
            //查询会议参会人
            MeetingManagement meetingManagement = meetingManagementService.selectMeetingManagementById(meetingCollect.getMeetingId());
            //参会人类型（0:组织党员 1:指定党员 2:全体党员）
            String meetingAttendeeType = meetingManagement.getMeetingAttendeeType();
            if("0".equals(meetingAttendeeType)){
                //本组织党员
                SysUser user = new SysUser();
                user.setDeptId(meetingManagement.getDeptId());
                List<SysUser> sysUsers = sysUserService.selectUserList(user);
                userIdList = sysUsers.stream().map(o -> o.getUserId().toString()).collect(Collectors.toList());
            }else if("1".equals(meetingAttendeeType)){
                //指定党员
                String meetingAttendee = meetingManagement.getMeetingAttendee();
                if(StringUtils.isNotBlank(meetingAttendee)){
                    userIdList = Arrays.asList(meetingAttendee.split(","));
                }
            }else if("2".equals(meetingAttendeeType)){
                //全体党员
                SysUser user = new SysUser();
                List<SysUser> sysUsers = sysUserService.selectUserList(user);
                userIdList = sysUsers.stream().map(o -> o.getUserId().toString()).collect(Collectors.toList());
            }
        }
        //指定会议内党员
        else if("1".equals(collectUserType)){
            userIdList = meetingCollect.getCollectUserIdList();
            String collectUser = StringUtils.join(userIdList, ",");
            meetingCollect.setCollectUser(collectUser);
        }
        int i = meetingCollectMapper.insertMeetingCollect(meetingCollect);

        //添加人员收集信息
        for(String userId : userIdList){
            MeetingCollectUser meetingCollectUser = new MeetingCollectUser();
            meetingCollectUser.setUserId(Long.valueOf(userId));
            meetingCollectUser.setCollectId(meetingCollect.getId());
            meetingCollectUserService.insertMeetingCollectUser(meetingCollectUser);
        }
        return i;
    }

    /**
     * 修改会议收集
     *
     * @param meetingCollect 会议收集
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateMeetingCollect(MeetingCollect meetingCollect)
    {
        meetingCollect.setUpdateTime(DateUtils.getNowDate());
        //修改收集人员
        Long id = meetingCollect.getId();
        MeetingCollect meetingCollectVo = selectMeetingCollectById(id);
        String collectUserType = meetingCollect.getCollectUserType();
        String collectUserTypeVo = meetingCollectVo.getCollectUserType();
        //指定参会人员
        if("1".equals(collectUserType)&&collectUserType.equals(collectUserTypeVo)){
            List<String> collectUserIdList = meetingCollect.getCollectUserIdList();
            List<String> collectUserIdListVo = meetingCollectVo.getCollectUserIdList();
            //修改的人员和之前的人员不一致
            List<Long> sortedList = collectUserIdList.stream().map(Long::valueOf).sorted().collect(Collectors.toList());
            List<Long> sortedListVo = collectUserIdListVo.stream().map(Long::valueOf).sorted().collect(Collectors.toList());
            String lstStr1 = StringUtils.join(sortedList, ",");
            String lstStr2 = StringUtils.join(sortedListVo, ",");
            if(!lstStr1.equals(lstStr2)){
                meetingCollectUserMapper.deleteMeetingCollectUserByCollectId(id);
                for(String userId : collectUserIdList){
                    MeetingCollectUser meetingCollectUser = new MeetingCollectUser();
                    meetingCollectUser.setUserId(Long.valueOf(userId));
                    meetingCollectUser.setCollectId(id);
                    meetingCollectUser.setCreateTime(DateUtils.getNowDate());
                    meetingCollectUserMapper.insertMeetingCollectUser(meetingCollectUser);
                }
                meetingCollect.setCollectUser(lstStr1);
            }
        }
        return meetingCollectMapper.updateMeetingCollect(meetingCollect);
    }

    /**
     * 删除会议收集信息
     *
     * @param id 会议收集主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteMeetingCollectById(Long id)
    {
        int i = meetingCollectMapper.deleteMeetingCollectById(id);
        //删除会议收集人信息
        MeetingCollectUser meetingCollectUser = new MeetingCollectUser();
        meetingCollectUser.setCollectId(id);
        List<MeetingCollectUser> meetingCollectUsers = meetingCollectUserService.selectMeetingCollectUserList(meetingCollectUser);
        for(MeetingCollectUser vo : meetingCollectUsers){
            meetingCollectUserService.deleteMeetingCollectUserById(vo.getId());
        }
        return i;
    }

    /**
     * 修改会议收集状态
     *
     * @param meetingCollect 会议收集
     * @return 结果
     */
    @Override
    public int updateMeetingCollectStatus(MeetingCollect meetingCollect){
        return meetingCollectMapper.updateMeetingCollect(meetingCollect);
    }


    /**
     * 查询会后收集待办任务
     *
     * @return 结果
     */
    @Override
    public List<UserUnDoVo> selectMeetingCollectTask(UserUnDoVo userUnDoVo){
        return meetingCollectMapper.selectMeetingCollectTask(userUnDoVo);
    }
}
